package fr.orleans.tournament;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ValueChangeEvent;

import entities.Match;
import entities.Referee;
import entities.Tournament;
import enums.MatchStatus;
import fr.orleans.tools.JSFServices;
import fr.orleans.tools.ManagedBeans;

@ManagedBean
@ViewScoped
public class AdminEdit extends ManagedBeans{

	private Tournament tournament;
	private List<Referee> listReferee;
	private List<Match> listMatchsInPreparation;
	private Referee newReferee;

	public AdminEdit(){
		try{
			this.tournament = (Tournament) JSFServices.getParameterInRequestMap("tournament");
		}catch(Exception e){
			
		}
	}

	public Tournament getTournament() {
		if(tournament == null){
			tournament = (Tournament) JSFServices.getParameterInRequestMap("tournament");
		}
		return tournament;
	}

	public void setTournament(Tournament tournament) {
		this.tournament = tournament;
	}

	public List<Match> getListMatchsInPreparation() {
		if(listMatchsInPreparation == null)
			listMatchsInPreparation = this.getService().getWebAction().getListMatchByFlagActivatedByTournamentAndByStatus(tournament, MatchStatus.preparation);
		
		return listMatchsInPreparation;
	}

	public void setListMatchsInPreparation(List<Match> listMatchs) {
		this.listMatchsInPreparation = listMatchs;
	}

	public Referee getNewReferee() {
		if(newReferee == null)
			newReferee = new Referee();
		return newReferee;
	}

	public void setNewReferee(Referee newReferee) {
		this.newReferee = newReferee;
	}

	public String addReferee(){
		newReferee.setListMatchs(new HashSet<Match>());
		newReferee.setTournament(tournament);
		
		this.listReferee.add(newReferee);
		this.tournament.setListReferees(new HashSet<>(this.listReferee));
		this.getService().getWebAction().editTournament(this.tournament);
		
		return "";
	}

	public String deleteReferee(Referee oneReferee){
		for(Match match : oneReferee.getListMatchs()){
			match.setReferee(null);
			this.getService().getWebAction().editMatch(match);
		}
		
		this.listReferee.remove(oneReferee);
		this.tournament.setListReferees(new HashSet<>(this.listReferee));
		this.getService().getWebAction().editTournament(this.tournament);
		
		return "";
	}

	public void assignRefereeToMatch(ValueChangeEvent event){
		//Match match = this.getService().getWebAction().findMatch(getIdMatch());
		Match match = this.getService().getWebAction().findMatch(event.getComponent().getAttributes().get("idMatch"));
		Referee newReferee = this.getService().getWebAction().findReferee(Long.parseLong((String) event.getNewValue()));
		Referee oldReferee = null;
		if(match.getReferee() != null){
			oldReferee = match.getReferee();
		}
		match.setReferee(newReferee);
		newReferee.getListMatchs().add(match);
		this.getService().getWebAction().editMatch(match);
		this.getService().getWebAction().editReferee(newReferee);
		if(oldReferee != null){
			oldReferee.getListMatchs().remove(match);
			this.getService().getWebAction().editReferee(oldReferee);
		}
//		listReferees = this.getService().getWebAction().getListRefereeByFlagActivatedByTournament(tournament);
		//return "/modules/tournament/adminEdit.jsf";
	}

	public List<Referee> getListReferee() {
		if(this.listReferee == null)
			this.listReferee = new ArrayList<>(this.tournament.getListReferees());
		return listReferee;
	}

	public void setListReferee(List<Referee> listReferee) {
		this.listReferee = listReferee;
	}
}